<?php

namespace Xrfg\Api\Trading\Spot\Listeners;

use Xrfg\Api\Trading\Spot\Events\TradeSavedEvent;

class UpdateTrade
{
    protected $redis;

    public function __construct()
    {
        $this->redis = app('redis.connection');
    }

    public function handle(TradeSavedEvent $event)
    {
        $trade = $event->trade;
        $tradeDetail = [
            'id' => $trade->id,
            'side' => $trade->side,
            'price' => $trade->price,
            'number' => $trade->number,
            'time' => $trade->created_at->timestamp,
        ];
        // 更新Redis的成交记录
        $key = "etf:trade:{$trade->symbol}";
        $this->redis->lpush($key, json_encode($tradeDetail));
        // 保留100条记录
        $this->redis->ltrim($key, 0, 99);
    }
}
